"""Biweight

.. helpdoc::
Biweight estimation for matricies.
"""



"""
<widgetXML>
<name>Biweight</name>
    <icon>default.png</icon>
    <tags> 
        <tag>Microarray</tag> 
    </tags>
    <summary>Biweight estimation for matricies.</summary>
    <details>Biweight estimation for matricies.</details>
    
    
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""
## biweight esetimation.  A widget that runs biweight estimation on a set of data.

## RDataFrame to RMatrix.  

"""
<name>Biweight</name>
<description>Biweight estimation for matricies.</description>
<tags>Bioinformatics</tags>
<RFunctions></RFunctions>
<icon></icon>
"""


from OWRpy import *
# import OWGUI
import signals
import redRGUI 
import redREnviron
import libraries.base.signalClasses.RMatrix as rmat
import libraries.base.signalClasses.RModelFit as rmf

class biweightEst(OWRpy):
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["biweight"])
        self.data = None
        
        """.. rrsignals::"""
        self.inputs.addInput('Data', 'Data', rmat.RMatrix, self.gotData)
        
        
        """.. rrsignals::"""
        self.outputs.addOutput('Data', 'Data', rmf.RModelFit)
        
        self.require_librarys(['rrcov', 'MASS'])
        
        self.R('source(\''+unicode(os.path.join(redREnviron.directoryNames['libraryDir'], 'affy', 'biweight.r')).replace('\\', '//')+'\')')
        ## GUI ##
        
        widgetArea = redRWidgetBox(self.controlArea)
        outputArea = redRWidgetBox(self.controlArea)
        
        self.infoA = redRWidgetLabel(widgetArea, 'No Data Connected')
        self.r = redRLineEdit(widgetArea, label = 'Breakdown', text = '0.2', toolTip = 'The breakdown value')
        
        
        self.outputText = redRTextEdit(outputArea)
        
    def gotData(self, data):
        if data:
            self.data = data
            self.process()
        else:
            self.data = None
    
        
    def process(self):
        if self.data:
            self.R('r<-'+str(self.r.text()), wantType = 'NoConversion')
            self.R('n1<-length('+self.data.getData()+'[,1])', wantType = 'NoConversion')
            self.R('p<-length('+self.data.getData()+'[1,])', wantType = 'NoConversion')
            self.R('c1<-rejpt.bw(p, r)[1]', wantType = 'NoConversion')
            self.R('b0<-erho.bw(p, c1)[1]', wantType = 'NoConversion')
            self.R('samp.mcd <- covMcd('+self.data.getData()+') #you need rrcov', wantType = 'NoConversion')
            self.R(self.Rvariables['biweight']+'<-biwt.est('+self.data.getData()+', n1, p, r, c1, b0, samp.mcd)', wantType = 'NoConversion')
            
            newData = rmf.RModelFit(data = self.Rvariables['biweight'])
            self.rSend('data', newData)
            
            self.outputText.setPlainText(unicode(self.R('unicode('+self.Rvariables['biweight']+')')))
            
            
"""
r<-0.2 # breakdown
n1<-30 # number of samples

c1<-rejpt.bw(p=2,r)[1]
b0<-erho.bw(p=2,c1)[1]

samp.mcd <- covMcd(samp.data) #you need rrcov
samp.bw <- biwt.est(samp.data,n1,p=2,r,c1,b0,samp.mcd)
samp.bw.corr <- samp.bw$biwt.sig[1,2] / sqrt(samp.bw$biwt.sig[1,1]*samp.bw$biwt.sig[2,2])

"""